<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
	<head>
		<meta charset="utf-8">
		<title>layuiAdmin 网站用户</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
		<link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
		<script type="text/javascript" th:src="@{/public/jquery-3.4.1.js}"></script>
	</head>
	<body>
		<div class="layui-fluid">
			<div class="layui-card">
				<!-- 搜索栏div -->
				<div class="layui-form layui-card-header layuiadmin-card-header-auto">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">选择药品</label>
							<div class="layui-input-block">
			                    <select type="text" name="drug.drugDict_id" autocomplete="off" class="layui-select" lay-search>
			                        <option value="">未选择</option>
			                        <option th:each="drug:${drugs}" th:value="${drug.drugDict_id}">[[${drug.drugDict_name}]]</option>
			                    </select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">输入药品名</label><!--   -->
							<div class="layui-input-block">
								<input type="text" name="drug.drugDict_name" id="drugDict_name" oninput="search(this)" class="layui-input" placeholder="模糊搜索" autocomplete="off"  
									style="position:absolute;z-index:2;width:86%;">
			                    <select type="text" id="SelectId-drugDict_name" lay-filter="TextSelect-lay-filter" autocomplete="off" class="layui-select" lay-search>
			                        <option value="">清空</option>
			                        <option th:each="drug:${drugs}" th:value="${drug.drugDict_id}">[[${drug.drugDict_name}]]</option>
			                    </select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">批次</label>
							<div class="layui-input-block">
								<input type="text" name="instock.is_batch" id="is_batch" oninput="search(this)" class="layui-input" placeholder="模糊搜索" autocomplete="off" 
									style="position:absolute;z-index:2;width:86%;" >
			                    <select type="text" id="SelectId-is_batch" lay-filter="TextSelect-lay-filter" autocomplete="off" class="layui-select" lay-search>
			                        <option value="">清空</option>
			                        <option th:each="item:${instocks}" th:value="${item.is_id}">[[${item.is_batch}]]</option>
			                    </select>
							</div>
						</div>
						
						<div class="layui-inline" style="margin-right: 0px;">
							<label class="layui-form-label">数量</label>
							<div class="layui-input-inline" style="width: 92px;">
								<input type="text" name="re_num_min" lay-verify="gtZeroOrNull" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
							</div>
							<div class="layui-form-mid">~</div>
							<div class="layui-input-inline" style="width: 92px;">
								<input type="text" name="re_num_max" lay-verify="gtZeroOrNull" placeholder="" autocomplete="off" class="layui-input">
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">来自--</label>
							<div class="layui-input-block">
								<select name="re_source">
									<option value="">未选择</option>
									<option th:each="item:${sources}" th:value="${item.name()}">[[${item.name()}]]</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">来自库房</label>
							<div class="layui-input-block">
								<select name="fromstorehouse.store_id">
									<option value="">未选择</option>
									<option th:each="item:${storehouses}" th:value="${item.store_id}">[[${item.store_name}]]</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">来自药房</label>
							<div class="layui-input-block">
								<select name="re_frompharmacyid">
									<option value="">未选择</option>
									<option th:each="item:${pharmacies}" th:value="${item.pharmacy_id}">[[${item.pharmacy_id}]]</option>
								</select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">原因</label>
							<div class="layui-input-block">
			                    <select type="text" name="re_reason" autocomplete="off" class="layui-select" lay-search>
			                        <option value="">清空</option>
			                        <option th:each="item:${reasons}" th:value="${item.name()}">[[${item.name()}]]</option>
			                    </select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">说明</label>
							<div class="layui-input-block">
								<input type="text" name="re_remark" id="re_remark" oninput="search(this)" class="layui-input" placeholder="模糊搜索" autocomplete="off" 
									style="position:absolute;z-index:2;width:86%;" >
			                    <select type="text" id="SelectId-re_remark" lay-filter="TextSelect-lay-filter" autocomplete="off" class="layui-select" lay-search>
			                        <option value="">清空</option>
			                        <option th:each="item:${recoveries}" th:value="${item.re_remark}">[[${item.re_remark}]]</option>
			                    </select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">申请人</label>
							<div class="layui-input-block">
								<select name="re_applicantid">
									<option value="">未选择</option>
									<option th:each="item:${users}" th:value="${item.user_id}">[[${item.realname}]]</option>
								</select>
							</div>
						</div>
						
						<div class="layui-inline" style="width: 398px;">
							<label class="layui-form-label">回收时间</label>
							<div class="layui-input-inline">
								<input type="text" name="re_recoverytime_interval" id="re_recoverytime_interval" 
									class="layui-input" placeholder=" - 区间 - " style="width: 294px;">
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">送往库房</label>
							<div class="layui-input-block">
								<select name="re_toroomid">
									<option value="">未选择</option>
									<option th:each="item:${storehouses}" th:value="${item.store_id}">[[${item.store_name}]]</option>
								</select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">状态</label>
							<div class="layui-input-block">
								<select name="re_state">
									<option value="">未选择</option>
									<option value="等待核实">等待核实</option>
									<option value="已回收">已回收</option>
									<option value="已取消">已取消</option>
								</select>
							</div>
						</div>
						
						<div class="layui-inline">
							<label class="layui-form-label">验收人</label>
							<div class="layui-input-block">
								<select name="re_confirmid">
									<option value="">未选择</option>
									<option th:each="item:${users}" th:value="${item.user_id}">[[${item.realname}]]</option>
								</select>
							</div>
						</div>
						
						<div class="layui-inline" style="width: 398px;">
							<label class="layui-form-label">验收时间</label>
							<div class="layui-input-inline">
								<input type="text" name="re_confirmtime_interval" id="re_confirmtime_interval" 
									class="layui-input" placeholder=" - 区间 - " style="width: 294px;">
							</div>
						</div>
						
						<!-- 按钮区域 -->
						<div class="layui-inline" style="margin-left: 39px;">
							<!-- 提交按钮 -->
							<button class="layui-btn btn-submit-recovery" lay-submit lay-filter="LAY-recovery-front-submit">
								<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
							</button>
							<!-- 重置按钮 -->
							<button class="layui-btn btn-submit-recovery" lay-submit lay-filter="LAY-recovery-front-reset">
								<i class="layui-icon layui-icon-refresh-3 layuiadmin-button-btn"></i>
							</button>
						</div>
					</div>
				</div>
				
				<div class="layui-card-body">
					<!-- 表格上的按钮组 -->
					<div style="padding-bottom: 10px;">
						<button class="layui-btn layui-btn-danger btn-submit-recovery" data-type="batchdel">删除</button>
						<button class="layui-btn btn-submit-recovery" data-type="add">添加</button>
					</div>

					<!-- 表格主题 -->
					<table id="recovery-table" lay-filter="recovery-table"></table>
				</div>
				
				<!-- 设置样式的js代码 -->
				<script type="text/html" id="imgTpl">
					<img style="display: inline-block; width: 50%; height: 100%;" src= {{ d.avatar }}>
        		</script>
       			<!-- 设置操作栏的按钮样式 -->
        		<script type="text/html" id="table-recovery-operatecolumn">
					{{#  if(d.re_state == '等待核实'){ }}
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" style="background-color:#6faeff;"><i class="layui-icon layui-icon-edit"></i>编辑</a>
					{{#  } else if(d.re_state != '等待核实'){ }}
						<a class="layui-btn layui-btn-xs" style="visibility: hidden;"><i class="layui-icon layui-icon-component"></i>占位</a>
					{{#  } }}
					<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        		</script>
        		<!-- 设置操作栏的按钮样式 -->
        		<script type="text/html" id="buttonTpl">
					{{#  if(d.re_state == '等待核实'){ }}
						<button class="layui-btn layui-btn-xs layui-btn-disabled">等待核实</button>
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="confirm"><i class="layui-icon layui-icon-ok-circle"></i>确认</a>
						<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="cancel"><i class="layui-icon layui-icon-close-fill"></i>取消</a>
					{{#  } else if(d.re_state == '已回收'){ }}
           				<button class="layui-btn layui-btn-xs layui-btn-primary">已回收</button>
					{{#  } else if(d.re_state == '已取消'){ }}
						<button class="layui-btn layui-btn-xs" style="background-color:#b5b5b5;">已取消</button>
					{{#  } }}
				</script>
			</div>
		</div>

		<!-- 引入layui框架 -->
		<script th:src="@{/layuiadmin/layui/layui.js}"></script>
		<!-- 引入公共layui代码 -->
		<script type="text/javascript" th:src="@{/public/warehouse/publicsetting.js}"></script>
		<script>
			//启动layui语法：config全局配置
			layui.config({
				base: '[[@{/layuiadmin/}]]' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'warehouse/recovery', 'table' ,'laydate'], function() {//调用layui的js文件，注意路径，直接在/layuiadmin/modules/文件夹下
				//生成标签的对应对象
				var $ = layui.$,
					form = layui.form,
					table = layui.table,
					laydate = layui.laydate;
				//监听重置搜索
				form.on('submit(LAY-recovery-front-reset)', function(data) {
					location.reload();
				});

				//监听搜索
				form.on('submit(LAY-recovery-front-submit)', function(data) {
					var field = data.field;

					//执行重载，重新加载表格数据
					table.reload('recovery-table', {
						where: field
					});
				});

				//事件。当标签的 data-type=""字段等于这里的字段名时，执行对应方法
				var active = {
					//批量删除按钮的事件
					batchdel: function() {
						var checkStatus = table.checkStatus('recovery-table'),//获得指定表格的选中行状态
							checkData = checkStatus.data; //得到选中行的数据
						if (checkData.length === 0) {//判断是否有选择行
							return layer.msg('请选择数据');
						}
						layer.prompt({//弹出输入口令窗口
							formType: 1,
							value: '',//默认值
							title: '敏感操作，请验证口令'
						}, function(value, index, elem) {//elem代表元素
							layer.close(index); //关闭口令窗口
							console.log(value); //得到口令窗口输入框的值

							layer.confirm('确定删除吗？', function(index) {
								console.log(checkData);
								//定义为数组
								var ids=[];
								$.each(checkData,function(key,item){
									console.log(key+" " +item+"        "+item.re_id);
									ids.push(item.re_id);//加入id值
								});
								$.ajax({
									url: getRootPath() + "/warehouse/recovery/delete",
									type:"POST",//请求方式(GET | POST)
									data:{
										ids,//键名称和@RequestParam("键[]") 的值相同
									},
									success:function(data){
										layer.msg(data);//提示信息
										console.log(data);
										table.reload('recovery-table');//重新加载表格
									},
								});
								//执行 Ajax 后重载
								/*
								admin.req({
								  url: 'xxx'
								  //,……
								});
								*/
							});
						});
					},
					//添加按钮的事件
					add: function() {
						layer.open({//弹窗功能，窗口内容为一个独立的网页
							type: 2,
							title: '新增回收单',
							content: '[[@{/warehouse/recovery/editpage}]]',//网页地址
							maxmin: true,
							area: ['660px', '660px'],//定义弹窗的大小
							btn: ['确定', '取消'],//定义弹窗的按钮
							yes: function(index, layero) {
								var iframeWindow = window['layui-layer-iframe' + index],
									submitID = 'LAY-recovery-front-submit',
									submit = layero.find('iframe').contents().find('#' + submitID);

								//监听提交
								iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
									var field = data.field; //获取提交的字段
									//提交 Ajax 成功后，静态更新表格中的数据
									$.ajax({
										url: getRootPath() + "/warehouse/recovery/insertone",
										type: "POST", //请求方式(GET | POST)
										contentType: 'application/json;charset=UTF-8', //发送信息的数据类型
										data: JSON.stringify(data.field),
										success: function(data) {
											console.log(data);
											if(data.indexOf('成功')!=-1){
												//重新加载表格
												table.reload('recovery-table', {});
												layer.msg(data);
											}else{
												//如果失败，弹出会一直保留的弹窗
												layer.alert(data);//提示信息
											}
										},
									});

									table.reload('LAY-recovery-front-submit'); //数据刷新
									layer.close(index); //关闭弹层
								});
								
								submit.trigger('click');
							}
						});
					}
				};
				
				laydate.render({
					elem: '#re_recoverytime_interval',
					type: "datetime",
					range: "~",
				});
				laydate.render({
					elem: '#re_confirmtime_interval',
					type: "datetime",
					range: "~",
				});
				
				//不知道是什么
				$('.layui-btn.btn-submit-recovery').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>
</html>
